package com.beiye.drivertransport.utils;


import android.graphics.Color;

import com.beiye.drivertransport.bean.UserCourseChartBean;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.util.ArrayList;
import java.util.List;


public class ChartUtilUseCourse {
        private static String CHECKJSON = "{\n" +
                "    \"rows\":[\n" +
                "        {\n" +
                "            \"NAME\":\"7月\",\n" +
                "            \"NUM\":\"10\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"NAME\":\"8月\",\n" +
                "            \"NUM\":\"26\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"NAME\":\"9月\",\n" +
                "            \"NUM\":\"58\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"NAME\":\"10月\",\n" +
                "            \"NUM\":\"60\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"NAME\":\"11月\",\n" +
                "            \"NUM\":\"60\"\n" +
                "        },\n" +
                "        {\n" +
                "            \"NAME\":\"12月\",\n" +
                "            \"NUM\":\"15\"\n" +
                "        }\n" +

                "    ]\n" +
                "}";


    public static  void initBarchart( BarChart check_bc){
            check_bc.getDescription().setEnabled(false);
            Description description = new Description();
            description.setText("");//设置右下角的描述信息
            description.setTextSize(15f);//设置描述信息字体大小
            check_bc.setScaleEnabled(true);
            check_bc.setDescription(description);
            check_bc.setNoDataText("暂无数据"); //设置空表的描述信息
            check_bc.setDrawGridBackground(false);//是否绘制网格背景
            check_bc.setDrawValueAboveBar(true);//将Y数据显示在点的上方
            check_bc.setPinchZoom(true);//挤压缩放
            check_bc.setDrawBarShadow(false);
            check_bc.setDrawGridBackground(false);
            check_bc.setScaleYEnabled(false);
            check_bc.setDoubleTapToZoomEnabled(false);//双击缩放
            check_bc.animateY(2500);
            check_bc.getLegend().setEnabled(false);
            check_bc.getAxisLeft().setEnabled(false);
            check_bc.getAxisRight().setEnabled(false);
    }
        public static void initCheckData(BarChart barChart,List<UserCourseChartBean.DataBean.StatListBean> statList){;
                        ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
                        ArrayList<String> xVals = new ArrayList<String>();
                        for(int j = 0;j<statList.size();j++){

                                BarEntry barEntry = new BarEntry(j,statList.get(j).getReadLength());
                                xVals.add( statList.get(j).getStatYm());
                                entries.add(barEntry);
                        }

                           setData(entries,xVals,barChart);



        }


        public static void setData(ArrayList<BarEntry> entries, final ArrayList<String> xVals, BarChart check_bc) {
                XAxis xAxis =  check_bc.getXAxis();
                xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
                xAxis.setDrawGridLines(false);
                xAxis.setTextColor(0xFF9d9d9f);
                //设置x轴的数据
                xAxis.setValueFormatter(new IAxisValueFormatter() {
                        @Override
                        public String getFormattedValue(float value, AxisBase axis) {
                                return String.valueOf(xVals.get((int) value));
                        }
                });
                //显示个数
                xAxis.setLabelCount(6);
                xAxis.setTextSize(10f);
                check_bc.moveViewToX(xVals.size() - 1);
                BarDataSet dataSet = new BarDataSet(entries, "");
                dataSet.setValueFormatter(new IValueFormatter() {
                        @Override
                        public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {


                                int n = (int) value;
                                if(n==0)
                                {
                                        return "";
                                }else{

                                        return n+"";
                                }
                        }
                });
                //数据和颜色
                ArrayList<Integer> colors = new ArrayList<Integer>();
                // 柱状图颜色
                for(int i=0;i<entries.size();i++)
                {
                        if(entries.get(i).getY()<60)
                        {
                                colors.add(Color.rgb(38,145,255));
                        }
                        else
                        {
                                colors.add(Color.rgb(38,145,255));
                        }

                }
//                colors.add(Color.rgb(48,184,245));
//                colors.add(Color.rgb(48,184,245));
//                colors.add(Color.rgb(48,184,245));
//                colors.add(Color.rgb(48,184,245));
//                colors.add(Color.rgb(48,184,245));
//                colors.add(Color.rgb(48,184,245));
                dataSet.setColors(colors);
                //   dataSet.setValueTextColors(colors);

                ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
                dataSets.add(dataSet);

                BarData bardata = new BarData(dataSets);
                bardata.setDrawValues(true);
                bardata.setValueTextColor(0xFF2691ff);
                bardata.setValueTextSize(10f);
                check_bc.setData(bardata);
//                check_bc.animateXY(500,500);//图表数据显示动画
                check_bc.setVisibleXRangeMaximum(6);//设置屏幕显示条数
                check_bc.invalidate();
        }




}
